import axios from "axios";
import router from './router'
import Element from 'element-ui'



const request = axios.create({
  baseURL: "http://localhost:8080",
  timeout: 5000,
  headers: {
    'Content-Type': "application/json; charset=utf-8"
  }
})

request.interceptors.request.use(config => {
  config.headers["authorization"] = localStorage.getItem('token')
  return config
})

request.interceptors.response.use(
  response => {
  let res = response.data;

  if(res.code === 200){
    return response
  }else{
    console.log(res.msg)
    if (res.msg){
      Element.Message.error(res.msg)
    }else {
      Element.Message.error('系统异常')
    }
    return Promise.reject(response.data.msg)
  }
},
  error => {
    if (error.response.data){
      error.message = error.response.data
    }

    if(error.response.status === 401){
      router.push("/login")
    }

    Element.Message.error(error.message,{duratuin:3000})
    return Promise.reject(error)
  }
)

export default request
